package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.base.service.impl.BaseServiceImpl;
import com.example.common.utils.ApiResult;
import com.example.dao.TPlanRecordDetailMapper;
import com.example.dao.TPlanRecordMapper;
import com.example.model.entity.*;
import com.example.model.query.TPlanRecordQuery;
import com.example.model.query.TWarningQuery;
import com.example.service.TPlanRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 预案执行记录表 Service接口实现类
 *
 * @author zhangr
 * @Description Created on 2020-12-23
 */
@Service
public class TPlanRecordServiceImpl extends BaseServiceImpl<TPlanRecordMapper,TPlanRecord> implements TPlanRecordService {

    @Autowired
    private TPlanRecordMapper planRecordMapper;

    @Autowired
    private TPlanRecordDetailMapper planRecordDetailMapper;

    @Override
    public ApiResult<TPlanRecordQuery> pageUserVoByQuery(TPlanRecordQuery query) {
        query.setOptimizeCountSql(false);
        Long current = query.getCurrent();
        query.setCurrent(1L);
        Integer total = planRecordMapper.countByQuery(query);
        query.setTotal(total);
        query.setCurrent(current);
        planRecordMapper.pageInfoByQuery(query);
        List list = query.getRecords();
        for (int i = 0; i <list.size() ; i++) {
            TPlanRecordQuery recordQuery =(TPlanRecordQuery) list.get(i);
            QueryWrapper<TPlanRecordDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TPlanRecordDetail::getIsValid,1L)
                    .eq(TPlanRecordDetail::getPlanRecordId,recordQuery.getPlanRecordId());
            List<TPlanRecordDetail> detailList = planRecordDetailMapper.selectList(queryWrapper);
            recordQuery.setDetailList(detailList);
        }
        return new ApiResult<>(query);
    }
}

